﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Camera))]
public class CameraCOM : MonoBehaviour
{
    [SerializeField]private BoyController boyController;
    [SerializeField]private BirdMoveController birdController;
    [SerializeField]private float followSpeed = 1f;
    private Transform target;
    private bool cBird=false;
    private Vector3 Boydisten;
    private Vector3 disten;

    // Start is called before the first frame update
    void Start()
    {
        target = boyController.transform;
        Boydisten = transform.position - target.position;
        disten = Boydisten;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (BirdState.Controllable(birdController.state)&&!cBird)
        {
            target = birdController.transform;
            cBird = true;
            //disten = transform.position - target.position;
        }
        else if(!BirdState.Controllable(birdController.state)&& cBird)
        {
            target = boyController.transform;
            cBird = false;
            disten=Boydisten;
        }
        Follow();
    }

    private void Follow()
    {
        if (Vector3.Distance(target.position + disten, transform.position)>7)
        {
            transform.position = Vector3.Lerp(transform.position, target.position + disten, followSpeed * Time.fixedDeltaTime*5);
        }
        transform.position = Vector3.Lerp(transform.position, target.position + disten, followSpeed * Time.fixedDeltaTime * 2);
    }

}
